import { Disposable, ICellData, IUniverInstanceService, UniverInstanceType, Workbook } from '@univerjs/core';

export class JnpfSheetsCellService extends Disposable {
  constructor(
    @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
    // @Inject(SheetsSelectionsService) private readonly _selectionManagerService: SheetsSelectionsService,
    // @ICommandService private readonly _commandService: ICommandService,
  ) {
    super();
  }

  getCellData(col: number, row: number) {
    const sheet = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet();
    const cellMatrix = sheet.getCellMatrix().getMatrix();

    return cellMatrix?.[row]?.[col] ?? null; // 如果未找到匹配的单元格，返回 null
  }

  setCellData(col: number, row: number, cellData: ICellData) {
    const sheet = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet();
    const cellMatrix = sheet.getCellMatrix();

    try {
      cellMatrix.setValue(row, col, cellData);

      return true;
    } catch (e) {
      console.warn(e);
      return false;
    }
  }

  refreshRangeCellsView(range: any) {
    if (!range) {
      return false;
    }

    const originalValue = range?.getValue() ?? '';
    return range?.setValue(originalValue);
  }
}
